<?php

namespace App\Handler;

use App\Models\Bulletin;
use App\Models\BulletinContent;
use App\Utils\StringUtils;
use p5\objects\ArrayUtils;


/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/12
 * Time: 9:36
 */
class BulletinHandler
{

    static public function saveContent($bulletinId, $content, $deleteFirst = false)
    {
        if ($deleteFirst) {
            BulletinContent::where("bulletin_id", $bulletinId)->delete();
        }
        $matrix = StringUtils::explodeByLen($content, 5000);
        $index = 0;
        foreach ($matrix as $string) {
            $bean = new BulletinContent();
            $bean->set_bulletin_id($bulletinId);
            $bean->set_content($string);
            $bean->set_sequence($index++);
            $bean->save();
        }
    }

    public static function loadContent($bulletin_id)
    {
        $list = BulletinContent::where("bulletin_id", $bulletin_id)->get();
        if (empty($list)) {
            return "";
        }
        $content = ArrayUtils::pick($list, "content");
        return implode("", $content);
    }


}